package com.colin.aiyi.ui.bannerDetail;

import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.colin.aiyi.R;
import com.colin.aiyi.app.Constant;
import com.colin.aiyi.ui.BaseActivity;
import com.colin.aiyi.view.ShareBottomDialog;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import me.shaohui.shareutil.WXUtil;

/**
 * Created by hl on 2017/6/15.
 */

public class BannerDetailAc extends BaseActivity {
    @BindView(R.id.text_banner_left)
    TextView textBannerLeft;
    @BindView(R.id.text_banner_title)
    TextView textBannerTitle;
    @BindView(R.id.text_banner_rigth)
    TextView textBannerRigth;
    @BindView(R.id.webview)
    WebView webView;
    @BindView(R.id.text_share)
    LinearLayout textShare;
    @BindView(R.id.text_no_data)
    TextView textNoData;
    private String url;
    private String title;
    private String headUrl;
    private long firstTime=0;
    private ShareBottomDialog dialog;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ac_web);
        ButterKnife.bind(this);
        user=getUser();
        textBannerLeft.setOnClickListener(this);
        textBannerTitle.setText("活动详情");
        int id = getIntent().getIntExtra("id", 0);
        title=  getIntent().getStringExtra("title");
        headUrl=  getIntent().getStringExtra("headurl");
        url = Constant.banner_detail + "?id=" + id;
        initView(id);

    }

    private void initView(int id) {
        webView.setWebChromeClient(new WebChromeClient());
        webView.setWebViewClient(new MyClient());
        WebSettings webSettings = webView.getSettings();
        webSettings.setUseWideViewPort(true); //将图片调整到适合webview的大小
        webSettings.setLoadWithOverviewMode(true); // 缩放至屏幕的大小
        webSettings.setSupportZoom(true); //支持缩放，默认为true。是下面那个的前提。
        webSettings.setBuiltInZoomControls(true); //设置内置的缩放控件。若为false，则该WebView不可缩放
        webSettings.setDisplayZoomControls(false); //隐藏原生的缩放控件
        webSettings.setJavaScriptEnabled(true);  //设置webview支持js
        webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK); //关闭webview中缓存
        webSettings.setAllowFileAccess(true); //设置可以访问文件
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true); //支持通过JS打开新窗口
        webSettings.setLoadsImagesAutomatically(true); //支持自动加载图片
        webSettings.setDefaultTextEncodingName("utf-8");//设置编码格式
        webSettings.setDomStorageEnabled(true); // 开启 DOM storagAPI 功能
        webSettings.setDatabaseEnabled(true);   //开启 database storage API 功能
        webSettings.setAppCacheEnabled(true);//开启 Application Caches 功能

        if (Constant.debug) {
            System.out.println(url);
        }
        webView.loadUrl(Constant.banner_detail + "?id=" + id);
    }


    class MyClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            webView.loadUrl(url);
            return true;
        }

        @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
            return shouldOverrideUrlLoading(view, request.getUrl().toString());
        }
    }

    @OnClick({R.id.text_share})
    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()){
            case R.id.text_share:
                if (isLoginWithLogin(user)){
                    if (System.currentTimeMillis() - firstTime <2000) {
                        firstTime = System.currentTimeMillis();
                        break;
                    }
                    if (dialog!=null&&dialog.isVisible()){
                        return;
                    }
                    new Thread(){
                        @Override
                        public void run() {
                            super.run();
                            dialog = new ShareBottomDialog();
                            dialog.setUrl(url);
                            dialog.setTitle(title);
                            //   dialog.setBitmap(BitmapFactory.decodeResource(getResources(),R.mipmap.app_logo));
                            dialog.setBitmap(WXUtil.getBitmap(Constant.BASE_URL+headUrl));
                            dialog.show(getSupportFragmentManager());
                        }
                    }.start();

                }
                break;
        }

    }
}
